<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
<!--    <meta name="viewport" content="width=device-width, initial-scale=1">-->
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">

    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="/dev/views/slx/layui/css/layui.css" rel="stylesheet">
    <link rel="stylesheet" href="/dev/plugins/bootstrap-3.4.1-dist/css/bootstrap.css">
    <link rel="stylesheet" href="/dev/views/slx/css/nav.css">
    <style>
        .demo-login-container {
            width: 320px;
            margin: 21px auto 0;
        }

        .demo-login-other .layui-icon {
            position: relative;
            display: inline-block;
            margin: 0 2px;
            top: 2px;
            font-size: 26px;
        }

        .wrap {
            width: 100%;
            height: 100%;
            /*background-image: url("/img/ico.jpg");*/
            background-repeat: no-repeat;
            background-size: cover;

        }

        .wrap img {
            position: absolute;
        }

        .imgStyle {
            width: 100%;
            height: 88.7%;
        }

        .demo-reg-container {
            width: 320px;
            margin: 21px auto 0;
        }

        .demo-reg-other .layui-icon {
            position: relative;
            display: inline-block;
            margin: 0 2px;
            top: 2px;
            font-size: 26px;
        }

        body {
            background-color: #9d9d9d;
            opacity: 0.75;
        }

        .wrap {
            width: 100%;
            height: 100%;
            /*background-image: url("/img/ioc.jpg");*/
            background-repeat: no-repeat;
            background-size: cover;
        }

        .zhuceForm {
            margin-left: 35%;
            margin-top: 5%;
            /*background-color: #cccccc;*/
            background-color: #e7e7e7;
            width: 400px;
            height: 440px;
            float: left;
            z-index: 9999;
            position: fixed;
            opacity: 0.75;
            border-radius: 5%;
        }
        #loginf{
            margin-left: 35%;
            margin-top: 10%;
            /*background-color: #cccccc;*/
            background-color: #e7e7e7;
            width: 400px;
            height: 227px;
            float: left;
            z-index: 9999;
            position: fixed;
            opacity: 0.75;
            border-radius: 5%;
        }


        /*body{*/
        /*    background-color: #9d9d9d;*/
        /*    opacity: 0.75;*/
        /*}*/
    </style>
</head>
<body>
<div id="app"></div>
<div class="wrap">
    <img src="/dev/views/slx/img/bj1.jpg" class="imgStyle">

    <div class="loginForm" id="loginf" style="display: block">
        <form class="layui-form">
            <div class="demo-login-container">
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-col-xs7">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-cellphone"></i>
                                </div>
                                <input type="text" name="cellphone" value="" lay-verify="required|phone"
                                       placeholder="手机号" lay-reqtext="请填写手机号" autocomplete="off" class="layui-input"
                                       id="reg-cellphone1">
                            </div>
                        </div>
                        <div class="layui-col-xs5">
                            <div style="margin-left: 11px;">
                                <button type="button" class="layui-btn layui-btn-fluid layui-btn-primary"
                                        lay-on="reg-get-vercode1" id="yzbtn">获取验证码
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-vercode"></i>
                        </div>
                        <input type="text" name="vercode" value="" lay-verify="required" placeholder="验证码"
                               lay-reqtext="请填写验证码" autocomplete="off" class="layui-input" id="code">
                    </div>

                </div>
                <div class="layui-form-item">
                    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="login">登录</button>
                </div>
                <hr class="layui-bg-green"/>
                <p><a href="javascript:void(0);" onclick="register()" class="fr" style="margin-left: 257px;">立即注册</a></p><br>
            </div>
        </form>
    </div>
    <div class="zhuceForm" id="register-form" style="display: none">
        <form class="layui-form">
            <div class="demo-reg-container">
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-col-xs7">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-cellphone"></i>
                                </div>
                                <input type="text" name="cellphone" value="" lay-verify="required|phone"
                                       placeholder="手机号" lay-reqtext="请填写手机号" autocomplete="off" class="layui-input"
                                       id="reg-cellphone">
                            </div>
                        </div>
                        <div class="layui-col-xs5">
                            <div style="margin-left: 11px;">
                                <button type="button" class="layui-btn layui-btn-fluid layui-btn-primary"
                                        lay-on="reg-get-vercode">获取验证码
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-vercode"></i>
                        </div>
                        <input type="text" name="vercode" value="" lay-verify="required" placeholder="验证码"
                               lay-reqtext="请填写验证码" autocomplete="off" class="layui-input" id="code1">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-password"></i>
                        </div>
                        <input type="password" name="password" value="" lay-verify="required|password" placeholder="密码"
                               autocomplete="off" class="layui-input" id="reg-password" lay-affix="eye">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-password"></i>
                        </div>
                        <input type="password" name="confirmPassword" value="" lay-verify="required|confirmPassword"
                               placeholder="确认密码" autocomplete="off" class="layui-input" lay-affix="eye">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-username"></i>
                        </div>
                        <input type="text" name="nickname" id="username" value="" lay-verify="required"
                               placeholder="用户名"
                               autocomplete="off" class="layui-input" lay-affix="clear">
                    </div>
                </div>
                <div class="layui-form-item">
                    <input type="checkbox" name="agreement" lay-verify="required" lay-skin="primary" title="同意">
                    <a href="#terms" target="_blank" style="position: relative; top: 6px; left: -15px;">
                        <ins>用户协议</ins>
                    </a>
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-reg">登录</button>
                </div>
                <hr class="layui-bg-orange"/>
                <p><a href="javascript:void(0);" onclick="login()" class="fr" style="margin-left: 160px;">已有账号？立即登录</a></p><br>
            </div>
        </form>
    </div>
</div>
<!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
<script src="/dev/plugins/jquery.min.js"></script>
<script src="/dev/plugins/bootstrap-3.4.1-dist/js/bootstrap.js"></script>
<script src="/dev/views/slx/layui/layui.js"></script>
<script>
    $(function () {
        $("#app").load('/dev/views/slx/nav.html');
    })

    function register() {
        var login = document.getElementById("loginf");
        var register = document.getElementById("register-form");
        login.style.display = 'none';
        register.style.display = 'block';
    }

    function login() {
        var login = document.getElementById("loginf");
        var register = document.getElementById("register-form");
        login.style.display = 'block';
        register.style.display = 'none';
    }


    //登录
    layui.use(['form','layer','jquery'], function () {
        var $ = layui.$;
        var form = layui.form;
        var layer = layui.layer;
        var util = layui.util;
        form.on('submit(login)',function (data) {
            // 获取数据
            // console.log(data.field);
            // layer.alert(JSON.stringify(data.field));

            let phone = $("#reg-cellphone1").val();
            console.log(phone);
            let code=$("#code").val();
            console.log(code);
            $.ajax({
                type: 'post',
                async: false,
                url: `/dev/user/login/${phone}/${code}`,
                success: function (result) {
                    localStorage.setItem("a", result)

                    let phone = document.getElementById("reg-cellphone1");
                    let code = document.getElementById("code");
                    phone.style.display = 'none'
                    // code.style.display = 'none'
                    if (result.code == 404) {
                        // phone.style.display = ''
                        // phone.innerText = layer.msg('qingxianzhuce');
                        // code.style.display = ''
                        // code.innerText =
                        layer.msg(result.msg);
                        setTimeout(function(){
                            window.location = '/dev/views/slx/login.html';
                        }, 3000);
                        // window.location = '/dev/views/slx/login.html'
                        // setTimeout(window.location = '/dev/views/slx/login.html',5000);


                    }else if(result.code == 200) {
                        // console.log(result.data+"================88")
                        localStorage.setItem("phone", result.data)
                        window.location = '/dev/views/slx/index.html';
                    }
                }
            })
            return false;
        });
        util.on('lay-on', {

            // 获取验证码
            'reg-get-vercode1': function (othis) {

                var isvalid = form.validate('#reg-cellphone1'); // 主动触发验证，v2.7.0 新增
                if (isvalid) {

                    layer.msg('手机号规则验证通过');
                    let phone = $('#reg-cellphone1').val();
                    $.post(`/dev/user/code/${phone}`, function (result) {
                        if (result.code == 200) {
                            layer.msg(result.msg);
                            // alert(result.msg)
                        }

                    })
                }
            }
        });
    });
//注册
    layui.use(function () {
        var $ = layui.$;
        var form = layui.form;
        var layer = layui.layer;
        var util = layui.util;

        // 自定义验证规则
        form.verify({
            // 确认密码
            confirmPassword: function (value, item) {
                var passwordValue = $('#reg-password').val();
                if (value !== passwordValue) {
                    return '两次密码输入不一致';
                }
            },
            password: function (value, elem) {
                // 自定义规则
                if (value.length < 6) {
                    return '不能小于 6 个字符';
                }
            }
        });

        // 提交事件
        form.on('submit(demo-reg)', function (data) {
            var field = data.field; // 获取表单字段值

            // 是否勾选同意
            if (!field.agreement) {
                layer.msg('您必须勾选同意用户协议才能注册');
                return false;
            }

            // 显示填写结果，仅作演示用
            // layer.alert(JSON.stringify(field), {
            //     title: '当前填写的字段值'
            // });

            // 此处可执行 Ajax 等操作
            // …

            //获取文本框中的数据
            let phone = $("#reg-cellphone").val();
            console.log(phone);
            let password = $("#reg-password").val();
            console.log(password);
            let username = $("#username").val();
            console.log(username)
            let code=$("#code1").val();
            console.log(code);
            $.ajax({
                type: 'post',
                async: false,
                url: `/dev/user/register/${phone}/${code}/${password}/${username}`,
                success: function (result) {
                    localStorage.setItem("a", result)
                    // console.log(result)
                    // console.log(10/0)
                    let username = document.getElementById("username");
                    let password = document.getElementById("reg-password");
                    let code = document.getElementById("code1");
                    username.style.display = 'none'
                    password.style.display = 'none'
                    if (result.code == 404) {
                        // username.style.display = ''
                        // username.innerText = alert("已注册请直接登录!")
                        layer.msg(result.msg);
                    } else if (result.code == 200) {
                        console.log(result.data)
                        localStorage.setItem("phone", result.data)
                        window.location = '/dev/views/slx/index.html';
                    }else if (result.code==500){
                        layer.msg(result.msg);
                        setTimeout(function(){
                            window.location = '/dev/views/slx/login.html';
                        }, 2000); // 设置5秒后执行跳转
                        // window.location = '/dev/views/slx/login.html';
                    }
                }
            })

            return false; // 阻止默认 form 跳转
        });

        // 普通事件
        util.on('lay-on', {
            // 获取验证码
            'reg-get-vercode': function (othis) {
                var isvalid = form.validate('#reg-cellphone'); // 主动触发验证，v2.7.0 新增
                if (isvalid) {
                    layer.msg('手机号规则验证通过');
                    let phone = $('#reg-cellphone').val();
                    $.post(`/dev/user/code1/${phone}`, function (result) {
                        if (result.code == 200) {
                            layer.msg(result.msg);
                            // alert(result.msg)
                        }else {
                            layer.msg(result.msg);
                            setTimeout(function(){
                                window.location = '/dev/views/slx/login.html';
                            }, 2000);
                        }

                    })
                }
            }
        });
    });
</script>

</body>
</html>

